<%@ WebHandler Language="C#" Class="imageUp" %>

using System;
using System.Web;
using System.IO;

public class imageUp : IHttpHandler, System.Web.SessionState.IRequiresSessionState
{
    public void ProcessRequest(HttpContext context)
    {
        context.Response.ContentType = "text/plain";
        context.Response.Charset = "utf-8";
        if (context.Session["User"] == null)
        {
            return;
        }
        //上传配置
        // String pathbase = "~/Upload/CMS/Images/" + DateTime.Now.ToString("yyyyMMdd") + "/";    
        String pathbase = "/Upload/CMS/Images/" + DateTime.Now.ToString("yyyyMMdd") + "/"; //保存路径
        string[] filetype = { ".gif", ".png", ".jpg", ".jpeg", ".bmp" };          //文件允许格式
        int size = 1024;                                                          //文件大小限制，单位KB

        //文件上传状态,初始默认成功，可选参数{"SUCCESS","ERROR","SIZE","TYPE"}
        String state = "SUCCESS";

        String title = String.Empty;
        String oriName = String.Empty;
        String filename = String.Empty;
        String url = String.Empty;
        String currentType = String.Empty;
        String uploadpath = String.Empty;

        uploadpath = context.Server.MapPath(pathbase);

        try
        {
            HttpPostedFile uploadFile = context.Request.Files[0];
            title = uploadFile.FileName;

            //目录验证
            if (!Directory.Exists(uploadpath))
            {
                Directory.CreateDirectory(uploadpath);
            }
            // pathbase = pathbase.Remove(0,13);
            //格式验证
            string[] temp = uploadFile.FileName.Split('.');
            currentType = "." + temp[temp.Length - 1].ToLower();
            if (Array.IndexOf(filetype, currentType) == -1)
            {
                state = "TYPE";
            }

            //大小验证
            if (uploadFile.ContentLength / 1024 > size)
            {
                state = "SIZE";
            }

            //保存图片
            if (state == "SUCCESS")
            {
                filename = title;
                if (File.Exists(uploadpath + filename))
                {
                    filename = title.Replace(currentType, "") + DateTime.Now.ToString("yyyyMMddhhmmss") + currentType;
                }
                //System.Drawing.Image image = System.Drawing.Image.FromStream(uploadFile.InputStream);
                //int newWidth = 600, newHeight = 200;
                //if ((decimal)image.Width > (decimal)newWidth)
                //{
                //    newWidth = 600;
                //    newHeight = Convert.ToInt32((decimal)image.Height * 600 / image.Width);


                //    System.Drawing.Bitmap bmp = new System.Drawing.Bitmap(newWidth, newHeight);
                //    System.Drawing.Graphics g = System.Drawing.Graphics.FromImage(bmp);
                //    g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality;
                //    g.CompositingQuality = System.Drawing.Drawing2D.CompositingQuality.HighQuality;
                //    g.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.High;
                //    System.Drawing.Rectangle rectDestination = new System.Drawing.Rectangle(0, 0, newWidth, newHeight);
                //    g.DrawImage(image, rectDestination, 0, 0, image.Width, image.Height, System.Drawing.GraphicsUnit.Pixel);
                //    bmp.Save(uploadpath + filename);
                //    bmp.Dispose();
                //    image.Dispose();
                //}
                //else
                //{
                    uploadFile.SaveAs(uploadpath + filename);
                //}

                //  Common.ImageTools.MakeThumbnail(uploadPath + fileName, uploadPath + "T_" + fileName, thumbImageSize.Width, thumbImageSize.Height, this.ThumbnailMode, InterpolationMode.High, SmoothingMode.HighQuality);
                url = pathbase + filename;
            }
        }
        catch (Exception)
        {
            state = "ERROR";
        }

        //获取图片描述
        if (context.Request.Form["pictitle"] != null)
        {
            if (!string.IsNullOrWhiteSpace(context.Request.Form["pictitle"]))
            {
                title = context.Request.Form["pictitle"];
            }
        }
        //获取原始文件名
        if (context.Request.Form["fileName"] != null)
        {
            if (!string.IsNullOrWhiteSpace(context.Request.Form["fileName"]))
            {
                oriName = context.Request.Form["fileName"].Split(',')[1];
            }
        }

        //向浏览器返回数据json数据
        HttpContext.Current.Response.Write("{'url':'" + url + "','title':'" + title.Replace(currentType, "") + "','original':'" + oriName + "','state':'" + state + "'}");
    }
    
    public bool IsReusable
    {
        get
        {
            return false;
        }
    }

}